Module# 15: String Class                                                                             Lecture#56: String Class

 

//   Example 56.1: Demonstration of String objects

 

import java.lang;          // It is not necessary as it is by default imported in all programs.

class BasicStringDemo {

    public static void main(String args[]) {

      String strOb1 = "First String";

      String strOb2 = "Second String";

      String strOb3 = strOb1 + " and " + strOb2;

      System.out.println(strOb1);

      System.out.println(strOb2);

      System.out.println(strOb3);

      String myString = “Joy with Java”;

      System.out.println(myString);

      System.out.println(“Welcome”);

      System.out.println(“Welcome” + “ “ + myString);

      String aString = “An example of string is ” + myString;

    }

}

 

//  Example 56.2: String as command line arguments

 

class CommandLineDemo {

     public static void main(String args[]) {

          System.out.println(“Number of arguments” + args.length);

          if(args.length == 0)

             return;

      for(int i=0; i<args.length; i++)

           System.out.println("args[" + i + "]: " + args[i]);

     }

}

 

//  Example 56.3: Creating string objects

 

class StringDemo {

     public static void main(String args[]) {

            String s1 = “N P T E L”;

      System.out.println(“length" + s1.length]);

      System.out.println(s1;

            String s2 = new String (“NPTEL”);

      System.out.println(s2 + “is of” + s2.length]);

      String s2 = new String (“NPTEL”);

            String s3 = new String (s1 + s2);

     }

}

 

// Example 56.4 : Create a String using literal and print

 

public class createStringDemo{

    public static void main(String args[]){

      String text = "DATA STRUCTURE WITH JAVA";

      System.out.print(text);

    }

}

 

// Example 56.5 : length() of String

 

public class StringLengthDemo{

    public static void main(String args[]){

      String text = "DATA STRUCTURE WITH JAVA";

      int stringLength = text.length();

      System.out.print(stringLength);

  }

}

 

// Example 56.6 : charAt()

 

public class CharAccessDemo{

    public static void main(String args[]){

      String text = "DATA STRUCTURE WITH JAVA";

      char data = text.charAt(4);

      System.out.print(data);

    }

}   

 

 

// Example 56.7 : concat()

 

public class StringMergingDemo{

    public static void main(String args[]){

      String text1 = "DATA STRUCTURE WITH";

      String text2 = " JAVA";

      String text3 = text1.concat(text2);

      System.out.print(text3);

    }

}

 

// Example 56.8 : toLowerCase()

 

public class StringToLowerCaseDemo{

    public static void main(String args[]){

      String text = "Data Structure with Java";

      String output = text.toLowerCase();

      System.out.println(output);

   }

}

 

// Example 56.9: toUpperCase()

 

public class StringToUpperCaseDemo{

    public static void main(String args[]){

      String text = "Data Structure with Java";

      String output = text.toUpperCase();

      System.out.println(output);

    }

}

 

// Example 56.10 : trim()

 

public class TrimStringDemo{

    public static void main(String args[]){

      String text = "   Data Structure with Java   ";

      String output = text.trim();

      System.out.println(output);

    }

}

 

// Example 56.11 : replace()

 

public class StringReplaceDemo{

   public static void main(String args[]){

      String text = "Data Structure with C++";

      String output = text.replace("C++" ,"Java");

      System.out.println(output);

   }

}